﻿// DigitalDesign
// Project: InformationBoard
// Developer: Syzdykov Sergey
// Created: 13.11.2011
// Last change: 19.11.2011
// Description: methods of locations page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GUIAdmin.AdminServiceReference;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace GUIAdmin.Views
{
    
    public partial class Locations : Page
    {
        private AdminServiceClient adminClient;
        private ObservableCollection<LocationNotEF> locationList;
        private int selectedIndex;
        
        public Locations()
        {
            InitializeComponent();
            adminClient = new AdminServiceClient();
            locationList = new ObservableCollection<LocationNotEF>();
            LocationList.ItemsSource = locationList;
            adminClient.GetListOfStationLocationsCompleted += FillLocationList;
            adminClient.AddLocationCompleted += updateLocationList;
            adminClient.UpdateLocationCompleted += updateLocationList;
            adminClient.DeleteLocationCompleted += updateLocationList;
        }
        // Выполняется, когда пользователь переходит на эту страницу.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ChangeLocation.Visibility = Visibility.Collapsed;
            
            if (DataContext != null)
                adminClient.GetListOfStationLocationsAsync((this.DataContext as StationNotEF).Id);
        }

        #region buttonClick
        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if (LocationList.SelectedItem != null)
            {
                adminClient.DeleteLocationAsync((LocationList.SelectedItem as LocationNotEF).Id);
            }

        }

        private void AddLocation_Click(object sender, RoutedEventArgs e)
        {
            showAddPanel();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (LocationList.SelectedItem != null && this.DataContext != null && LocationName.Text != "")
            {
                LocationNotEF locationNotEF = new LocationNotEF();
                locationNotEF.Name = LocationName.Text;
                locationNotEF.Id = (LocationList.SelectedItem as LocationNotEF).Id;
                locationNotEF.StationId = (this.DataContext as StationNotEF).Id;
                adminClient.UpdateLocationAsync(locationNotEF);
                LocationName.Focus();
            }

        }

        private void Create_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null && LocationName.Text != "")
            {
                LocationNotEF locationNotEF = new LocationNotEF();
                locationNotEF.Name = LocationName.Text;
                locationNotEF.StationId = (this.DataContext as StationNotEF).Id;
                adminClient.AddLocationAsync(locationNotEF);
                selectedIndex = -1;
                LocationName.Focus();
            }

        }

        private void LocationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            showChangePanel();
        }
        #endregion
        
        #region clientEvent
        private void FillLocationList(object sender, GetListOfStationLocationsCompletedEventArgs e)
        {
            locationList.Clear();
            foreach (var location in e.Result)
            {
                locationList.Add(location);
            }

            if (LocationList.Items.Count != 0 && selectedIndex != -1)
            {
                if (LocationList.Items.Count < selectedIndex + 1)
                    LocationList.SelectedIndex = LocationList.Items.Count - 1;
                else
                    LocationList.SelectedIndex = selectedIndex;
            }
            else
            {
                showAddPanel();
            }

        }

        private void updateLocationList(object sender, AsyncCompletedEventArgs e)
        {
            adminClient.GetListOfStationLocationsAsync((this.DataContext as StationNotEF).Id);
        }
        #endregion

        #region other
        private void showChangePanel()
        {
            Save.Visibility = Visibility.Visible;
            Create.Visibility = Visibility.Collapsed;
            ChangeLocation.Visibility = Visibility.Visible;
            if (LocationList.SelectedItem != null)
            {
                LocationName.Text = (LocationList.SelectedItem as LocationNotEF).Name;
                selectedIndex = LocationList.SelectedIndex;
                LocationName.Focus();
            }
        }

        private void showAddPanel()
        {
            Save.Visibility = Visibility.Collapsed;
            Create.Visibility = Visibility.Visible;
            ChangeLocation.Visibility = Visibility.Visible;
            LocationName.Text = "";
            LocationName.Focus();
        }

        #endregion
    }
}
